package main

import (
    "context"
    "encoding/csv"
    "fmt"
    "os"
  //  "path/filepath"
    "time"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
   "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"
)

// Access Key ID: AKLTY2FmZmI5Y2JhYmZmNDc3MmJmOTU3Yjg5Mzk5Y2U2YWM
// Secret Access Key: WVRVMVpEVmtOV1V6TWpRME5HWmxZVGxoTURCbU9HSXpNemRtTURNd1lUUQ==

// TOS Supported Endpoints:
// Endpoint: tos-cn-beijing.volces.com, Region: cn-beijing
// Endpoint: tos-cn-guangzhou.volces.com, Region: cn-guangzhou    <---
// Endpoint: tos-cn-shanghai.volces.com, Region: cn-shanghai
// Endpoint: tos-cn-beijing2.volces.com, Region: cn-beijing2
// Endpoint: tos-ap-southeast-1.volces.com, Region: ap-southeast-1

var (
    // 直接赋值访问信息
    endpoint  = "tos-cn-guangzhou.volces.com"
    region    = "cn-guangzhou"
    accessKey = "AKLTY2FmZmI5Y2JhYmZmNDc3MmJmOTU3Yjg5Mzk5Y2U2YWM"
    secretKey = "WVRVMVpEVmtOV1V6TWpRME5HWmxZVGxoTURCbU9HSXpNemRtTURNd1lUUQ=="
    //bucket    = os.Getenv("TOS_GO_SDK_BUCKET")
    bucketName = "dcp-upload-xyh" // 替换为实际的存储桶名称
    GET_PATH   = "avalon-red-set(series_3068)"
	ctx = context.Background()
)

func check(err error, message string) {
    if err != nil {
        fmt.Printf("%s err: %s\n", message, err.Error())
        os.Exit(1)
    }
}

func main() {
    // demo2func()
    // sampleFunction() // demo function .
    // listMyBuckets()
    getFileList(GET_PATH) // 替换为实际的路径
}

// getFileList 遍历指定路径下的所有文件，为每个文件生成一个可用于外部访问至少 7 天的 URL，
// 并将文件名和生成的 URL 按 CSV 格式输出到当前文件夹的 output[date-time].csv 文件中。
func getFileList(path string) {
    // 创建 ClientV2 实例
    client, err := tos.NewClientV2(endpoint, tos.WithRegion(region),
        tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
    check(err, "NewClientV2")

    // 打开 CSV 文件
    now := time.Now().Format("20060102150405")
    csvFile, err := os.Create(fmt.Sprintf("output%s.csv", now))
    check(err, "Create CSV file")
    defer csvFile.Close()

    // 创建 CSV 写入器
    writer := csv.NewWriter(csvFile)
    defer writer.Flush()

    // 写入 CSV 文件头
    err = writer.Write([]string{"文件名", "URL"})
    check(err, "Write CSV header")

    var continuationToken string
	var i int = 0
    for {
        // 调用 ListObjectsV2 方法
        output, err := client.ListObjectsV2(ctx, &tos.ListObjectsV2Input{
            Bucket:            bucketName,
            MaxKeys:           10,
            ContinuationToken: continuationToken,
        })
        check(err, "ListObjectsV2")

        // 遍历当前批次的对象
        for _, obj := range output.Contents {
            // 对象 Key
            fmt.Println("Object Key:", obj.Key)

            // 使用 CreatePresignedURL 方法生成预签名 URL
            req, err := client.PreSignedURL(&tos.PreSignedURLInput{
                HTTPMethod: enum.HttpMethodGet,
                Bucket:     bucketName,
                Key:        obj.Key, // 使用文件名作为 Key
            })
            check(err, "CreatePresignedURL")

            fmt.Println("PreSigned URL:", req.SignedUrl)
            fmt.Println("**** FILE No:%d **********************",i)
			i++

            // 写入 CSV 文件
            err = writer.Write([]string{obj.Key, req.SignedUrl})
            check(err, "Write CSV row")
        }

        // 如果没有更多的对象，退出循环
        if!output.IsTruncated {
            break
        }

        // 更新 continuationToken 以继续下一次列举
        continuationToken = output.NextContinuationToken
    }
}